﻿#region

using System.Web;
using HouseHoldBudget.Core.Authentication;
using HouseHoldBudget.Core.Entities.User;

#endregion

namespace HouseHoldBudget.Framework.Session
{
    public static class SessionPersister
    {
        #region Constants

        private const string usernameSessionVar = "CurrentUser";

        #endregion

        #region Class Properties

        /// <summary>
        ///   Gets or sets the username.
        /// </summary>
        /// <value> The username. </value>
        public static BudgetUser CurrentBudgetUser
        {
            get
            {
                if (CurrentBudgetUserIdentity == null)
                {
                    return null;
                }
                return CurrentBudgetUserIdentity.CurrentUser;
            }
        }

        /// <summary>
        ///   Gets or sets the username.
        /// </summary>
        /// <value> The username. </value>
        public static BudgetIdentity CurrentBudgetUserIdentity
        {
            get
            {
                if (HttpContext.Current == null)
                {
                    return null;
                }

                if (HttpContext.Current.Session == null)
                {
                    return null;
                }

                if (HttpContext.Current.Session[usernameSessionVar] != null)
                    return HttpContext.Current.Session[usernameSessionVar] as BudgetIdentity;
                return null;
            }
            set
            {
                if (HttpContext.Current.Session != null)
                    HttpContext.Current.Session[usernameSessionVar] = value;
            }
        }

        #endregion
    }
}